package edu.gznu.ics.fast.common.UIHelper;



import java.util.List;

/**
 * Created by fei-zyx on 2016/11/13.
 */
public abstract class PagiNationHelper<T1,T2> implements PagiNationHelperInterface  {

    @Override
    public abstract int getPageIndex(List inputVO) ;

    @Override
    public int checkIsExistNext(int pageSize, List inputVO, List resultVO) {
        int size = resultVO.size();

        int part = (int) (Math.ceil((double) size / (double) pageSize));
        int existNextPart;
        if (getPageIndex(inputVO) == part) {
            existNextPart = 0;
        } else {
            existNextPart = 1;
        }
        return existNextPart;
    }

    @Override
    public T2 makePagePartMessage(int part, int size, int existNextPart) {
        return null;
    }

    @Override
    public List<T2> produceMeassage(int pageSize, List inputVO, List resultVO) {
        int size = resultVO.size();
        int part = (int) (Math.ceil((double) size / (double) pageSize));
        List<T2> returnT2List;
        if (getPageIndex(inputVO) == -1)
            return resultVO;
        else {
            int begin = getPageIndex(inputVO) * pageSize;
            int end = (getPageIndex(inputVO) + 1) * pageSize;
            T2 pagePartMessage;
            if (end < size) {
                returnT2List = resultVO.subList(begin, end);
                pagePartMessage  = makePagePartMessage(part, size,1);
            } else {
                returnT2List = resultVO.subList(begin, size);
                 pagePartMessage = makePagePartMessage(part, size,0 );
            }

            returnT2List.add(pagePartMessage);
            return returnT2List;
        }
    }

    /* @Override
    public int getPageIndex(List<T1> inputVO) {
        if (inputVO.get(0).getQueryIndex() == 0)
            return -1;
        else
            return inputVO.get(0).getQueryIndex();
    }*/

   /* @Override
    public int checkIsExistNext(int pageSize, List<T1> inputVO, List<T2> resultVO) {
        int size = resultVO.size();

        int part = (int) (Math.ceil((double) size / (double) pageSize));
        int existNextPart;
        if (getPageIndex(inputVO) == part) {
            existNextPart = 0;
        } else {
            existNextPart = 1;
        }
        return existNextPart;
    }

    @Override
    public T2 makePagePartMessage(int part, int size, int existNextPart) {

        T2 PagePartMessageVO = new T2();
        PagePartMessageVO.setPart(part);
        PagePartMessageVO.setExistNextPart(existNextPart);
        PagePartMessageVO.setSize(size);
        return PagePartMessageVO;
    }

    @Override
    public List<T2> produceMeassage(int pageSize, List<T1> inputVO, List<T2> resultVO) {
        int size = resultVO.size();
        int part = (int) (Math.ceil((double) size / (double) pageSize));
        List<T2> returnT2List;
        if (getPageIndex(inputVO) == -1)
            return resultVO;
        else {
            int begin = getPageIndex(inputVO) * pageSize;
            int end = (getPageIndex(inputVO) + 1) * pageSize;
            if (end < size) {
                returnT2List = resultVO.subList(begin, end);
            } else {
                returnT2List = resultVO.subList(begin, size);
            }
            T2 PagePartMessageVO = makePagePartMessage(part, checkIsExistNext(pageSize, inputVO, resultVO), size);
            returnT2List.add(PagePartMessageVO);
            return returnT2List;
        }
    }*/
}
